/*
 * openTCS copyright information:
 * Copyright (c) 2005-2011 ifak e.V.
 * Copyright (c) 2012 Fraunhofer IML
 *
 * This program is free software and subject to the MIT license. (For details,
 * see the licensing information (LICENSE.txt) you should have received with
 * this copy of the software.)
 */
package org.opentcs.guing.components.dialogs;

import java.awt.Component;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.opentcs.guing.components.properties.type.ModelAttribute;
import org.opentcs.guing.components.properties.type.Property;

/**
 * Ein Dialog, in den eine Komponente vom Typ DialogContent zum Ändern von
 * Eigenschaftswerten hineingelegt werden.
 * Der Dialog verfügt über einen Ok- und einen Cancel-Button.
 *
 * @author Sebastian Naumann (ifak e.V. Magdeburg)
 */
public class StandardDetailsDialog
    extends javax.swing.JDialog
    implements DetailsDialog {

  /**
   * A return status code - returned if Cancel button has been pressed
   */
  public static final int RET_CANCEL = 0;
  /**
   * A return status code - returned if OK button has been pressed
   */
  public static final int RET_OK = 1;
  private int returnStatus = RET_CANCEL;
  /**
   * Die Komponente, die das Ändern eines Properties erlaubt.
   */
  private final DetailsDialogContent fContent;
  private final Component fParentComponent;

  /**
   * Creates new form JDialog
   *
   * @param parent Die Komponente, zu der der Dialog zentriert wird
   * @param content der Inhalt
   * @param modal ob der Dialog modal sein soll
   */
  public StandardDetailsDialog(JPanel parent, boolean modal, DetailsDialogContent content) {
    super(JOptionPane.getFrameForComponent(parent), modal);
    fContent = content;
    fParentComponent = parent;
    initialize();
  }

  /**
   * Konstruktor mit einem Dialog als Besitzer.
   *
   * @param parent
   * @param modal
   * @param content
   */
  public StandardDetailsDialog(JDialog parent, boolean modal, DetailsDialogContent content) {
    super(parent, modal);
    fContent = content;
    fParentComponent = parent;
    initialize();
  }

  /**
   * Initialisiert den Dialog.
   */
  protected final void initialize() {
    JComponent component = (JComponent) fContent;
    component.setBorder(new EmptyBorder(new java.awt.Insets(5, 5, 5, 5)));
    getContentPane().add(component, java.awt.BorderLayout.CENTER);
    initComponents();
    setTitle(fContent.getTitle());
    activate();
  }

  @Override
  public void activate() {
    getRootPane().setDefaultButton(okButton);
    pack();
    setLocationRelativeTo(fParentComponent);
  }

  public Component getParentComponent() {
    return fParentComponent;
  }

  /**
   * Gibt zurück, ob der Benutzer den Ok- oder den Cancel-Button gedrückt hat,
   * um dem Dialog zu beenden.
   *
   * @return the return status of this dialog - one of RET_OK or RET_CANCEL
   */
  public int getReturnStatus() {
    return returnStatus;
  }

  // CHECKSTYLE:OFF
  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    controlPanel = new javax.swing.JPanel();
    buttonPanel = new javax.swing.JPanel();
    okButton = new javax.swing.JButton();
    cancelButton = new CancelButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setIconImage(null);
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        closeDialog(evt);
      }
    });

    controlPanel.setLayout(new java.awt.BorderLayout());

    buttonPanel.setOpaque(false);

    okButton.setFont(okButton.getFont().deriveFont(okButton.getFont().getStyle() | java.awt.Font.BOLD));
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/opentcs/guing/res/labels"); // NOI18N
    okButton.setText(bundle.getString("dialog.buttonOk.text")); // NOI18N
    okButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        okButtonActionPerformed(evt);
      }
    });
    buttonPanel.add(okButton);

    cancelButton.setFont(cancelButton.getFont());
    cancelButton.setText(bundle.getString("dialog.buttonCancel.text")); // NOI18N
    cancelButton.setOpaque(false);
    cancelButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cancelButtonActionPerformed(evt);
      }
    });
    buttonPanel.add(cancelButton);

    controlPanel.add(buttonPanel, java.awt.BorderLayout.SOUTH);

    getContentPane().add(controlPanel, java.awt.BorderLayout.SOUTH);

    pack();
  }// </editor-fold>//GEN-END:initComponents
  // CHECKSTYLE:ON

  /**
   * Wird aufgerufen, wenn der Nutzer den Cancel-Button drückt.
   */
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
      doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButtonActionPerformed

  /**
   * Wird aufgerufen, wenn der Nutzer den OK-Button drückt.
   */
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
      fContent.updateValues();

      Property property = fContent.getProperty();

      if (property != null) {
        property.setChangeState(ModelAttribute.ChangeState.DETAIL_CHANGED);
      }

      doClose(RET_OK);
    }//GEN-LAST:event_okButtonActionPerformed

  /**
   * Schließt den Dialog.
   */
  private void doClose(int retStatus) {
    returnStatus = retStatus;

    setVisible(false);
  }

  /**
   * Closes the dialog
   */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
      doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog

  @Override
  public DetailsDialogContent getDialogContent() {
    return fContent;
  }

  // CHECKSTYLE:OFF
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JPanel buttonPanel;
  private javax.swing.JButton cancelButton;
  private javax.swing.JPanel controlPanel;
  private javax.swing.JButton okButton;
  // End of variables declaration//GEN-END:variables
  // CHECKSTYLE:ON
}
